package com.itcast.rpc.client.cluster.impl;


import com.itcast.rpc.client.channel.ProviderService;
import com.itcast.rpc.client.cluster.ClusterStrategy;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class WeightRandomClusterStrategyImpl implements ClusterStrategy {
    @Override
    public ProviderService select(List<ProviderService> services) {
        List<ProviderService> newProviderList = new ArrayList<>();
        for (ProviderService service : newProviderList) {
            int weight = service.getWeight();
            for (int i = 0; i < weight; i++) {
                newProviderList.add(service);
            }
        }
        int MAX_LEN = newProviderList.size();
        int index = RandomUtils.nextInt(0,MAX_LEN);
        return newProviderList.get(index);
    }
}
